<?php
/***************************************************************************
 * @Description:
 * 
 * @FileName:         Field.php
 * @Author :          liyong5@staff.sina.com.cn
 * @CreatedTime:      Mon 28 May 2018 06:15:45 PM CST
 ************************************************************************/
namespace app\modules\core\models;

use Yii;
use yii\base\Model;
use yii\validators\Validators;
use yii\mongodb\ActiveRecord;
use yii\base\InvalidArgumentException;

class Field extends ActiveRecord
{
    /**
     * 以下定义数据类型常量
     */
    const TYPE_INTEGER      = 'integer';

    const TYPE_STRING       = 'string';

    const TYPE_TEXT         = 'text';

    const TYPE_DATE         = 'date';

    const TYPE_TIME         = 'time';

    const TYPE_DATETIME     = 'datetime';

    const TYPE_TIMESTAMP    = 'timestamp';

    public static $fieldTypes = [
        self::TYPE_INTEGER      => '整型',
        self::TYPE_STRING       => '字符串',
        self::TYPE_TEXT         => '长文本',
        self::TYPE_DATE         => '日期(Y-M-D)',
        self::TYPE_TIME         => '时间(hh::mm:ss)',
        self::TYPE_DATETIME     => '时间(YY-MM-DD hh:mm:ss)',
        self::TYPE_TIMESTAMP    => '时间戳',
    ];

    /**
     * 以下定义数据源类型
     */
    const DATA_STYPE_AUTO   = 'auto';

    const DATA_STYPE_ENUM   = 'enum';

    const DATA_STYPE_FIX    = 'fix';

    const DATA_STYPE_REFER  = 'refer';

    public static $stypes = [
        self::DATA_STYPE_AUTO   => 'AUTO',
        self::DATA_STYPE_ENUM   => '列表',
        self::DATA_STYPE_FIX    => '固定值',
        self::DATA_STYPE_REFER  => '引用',
    ];

    public static $requiredList = [
        '1' => '是',
        '0' => '否',
    ];


    /**
     * String
     * 字段对应的表单控件类型
     * 使用 yii\bootstrap\ActiveForm 渲染字段, 可选值：
     * text-input       // 文本框
     * hidden-input     // 隐藏
     * password-input   // 密码框
     * file-input       // 文件
     * textarea         // 文本域
     * radio            // 单选
     * checkbox         // 复选
     * drop-down-list   // 下拉
     * list-box         //
     * checkbox-list    
     * radio-list
     */
    public static $viewList = [
        "text-input"       => "文本框",
        "hidden-input"     => "隐藏",
        "password-input"   => "密码框",
        "file-input"       => "文件",
        "textarea"         => "文本域",
        "radio"            => "单选",
        "checkbox"         => "复选",
        "drop-down-list"   => "下拉",
        "list-box"         => "",
        "checkbox-list"    => "",
        "radio-list"       => "",
    ];

    public $widget;

    /**
     * @inheritdoc
     */
    public static function collectionName()
    {
        return 'fields';
    }

    public function formName() {
        return '';
    }

    public function isRequired() {
        return $this->required ? true : false;
    }

    public function __toString() {
        return $this->toJson();
    }

    protected static function checkType($type) {
        if (! array_key_exists($type, self::$fieldTypes)) {
            return false;
        }
        return true;
    }

    public function rules() {
        return [
            [['id', 'type', 'name'], 'required'],
            [['name', 'desc'], 'string', 'min' => 2, 'max' => 15],
            ['classNs', 'string'],
            ['params', 'string'],
            ['required', 'boolean'],
            ['type', 'validateType'],
            ['view', 'validateView'],
            ['validates', 'default', 'value' => []],
            ['sourceType', 'validateSourceType'],
            ['source', 'validateSource'],
        ];
    }

    public function validateType($attr, $params) {
        if (! array_key_exists($this->type, self::$fieldTypes)) {
            return false;
        }
        return true;
    }

    public function validateView($attr, $params) {
        if (! array_key_exists($this->view, self::$viewList)) {
            return false;
        }
        return true;
    }

    public function validateSourceType($attr, $params) {
        if (! array_key_exists($this->sourceType, self::$stypes)) {
            return false;
        }
        return true;
    }

    public function validateSource($attr, $params) {
        return true;
    }

    public function validaters() {
        // 获取所有验证器，排除禁用的
        return [
            
        ];
    }

    public function attributes() {
        return ['_id', 'type', 'id', 'name', 'desc', 'classNs', 'params', 'view', 'required', 'sort', 'sourceType', 'source', 'validates', 'filters', 'author', 'ctime'];
    }

    public function attributeLabels() {
        return [
            'id'            => 'ID',
            'type'          => '数据类型',
            'name'          => '字段名',
            'desc'          => '字段描述',
            'classNs'       => '命名空间',
            'view'          => '视图组件',
            'required'      => '是否必需',
            'sourceType'    => '源类型',
            'source'        => '源数据',
            'validates'     => '验证器',
            'filters'       => '过滤器',
            'params'        => '参数',
        ];
    }

    public function createField() {
        if (! $this->classNs || ! class_exists($this->classNs)) {
            throw new Exception('classs not exists');
        }

        $Yii::createObject($this->classNs, $this->params);
        

        // $attr['id'] = $field
    }
}
